package com.googlecode.yami;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

public class MainMenu extends JMenuBar {

    private String[] fileItems = new String[] { "New Tab", "New Console",
	    "Close Tab", "Quit" };
    private String[] aboutItems = new String[] { "About YAMI" };
    private char[] fileShortcuts = { 'N', 'O', 'W', 'Q' };
    private char[] aboutShortcuts = { 'A' };

    MainMenu(final Connection conn) {

	JMenu fileMenu = new JMenu("File");
	JMenu aboutMenu = new JMenu("About");

	ActionListener performListener = new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if (command == "New Tab") {

		    new BoxComponent(conn);

		} else if (command == "New Console") {

		    new ConsoleComponent(conn);

		} else if (command == "Close Tab") {

		    Component current = MainWindow.JTP.getSelectedComponent();
		    MainWindow.JTP.remove(current);

		} else if ( command == "About YAMI" ) {
		    new AboutYami();
		} else {
		    try {
			conn.close();
		    } catch (SQLException sqlex) {
			System.out.println("Cannot close connection.");
		    }
		    System.exit(0);
		}
	    }
	};
	// FILE menu

	fileMenu.setMnemonic(KeyEvent.VK_F);
	aboutMenu.setMnemonic(KeyEvent.VK_A);

	for (int i = 0; i < fileItems.length; i++) {
	    JMenuItem fileMenuItem = new JMenuItem(fileItems[i],
		    fileShortcuts[i]);
	    fileMenuItem.addActionListener(performListener);
	    if (i == 0)
		fileMenuItem.setAccelerator(KeyStroke.getKeyStroke(
			KeyEvent.VK_N, InputEvent.CTRL_MASK));
	    else if (i == 1)
		fileMenuItem.setAccelerator(KeyStroke.getKeyStroke(
			KeyEvent.VK_O, InputEvent.CTRL_MASK));
	    else if (i == 2)
		fileMenuItem.setAccelerator(KeyStroke.getKeyStroke(
			KeyEvent.VK_W, InputEvent.CTRL_MASK));
	    else
		fileMenuItem.setAccelerator(KeyStroke.getKeyStroke(
			KeyEvent.VK_Q, InputEvent.CTRL_MASK));
	    fileMenu.add(fileMenuItem);
	}
	// ABOUT menu
	for (int i = 0; i < aboutItems.length; i++) {
	    JMenuItem aboutMenuItem = new JMenuItem(aboutItems[i],
		    aboutShortcuts[i]);
	    aboutMenuItem.addActionListener(performListener);
	    aboutMenu.add(aboutMenuItem);
	}

	add(fileMenu);
	add(aboutMenu);

    }
}

class AboutYami extends JDialog {

    public AboutYami() {
	JFrame f = new JFrame();
	JOptionPane
		.showMessageDialog(
			f,
			"YAMI - Yet Another MySql Interface"
				+ "\n\nVersion: "
				+ MainWindow.VERSION
				+ "\n\nDevelopers:"
				+ "\nKőműves Ákos\n\n"
				+ "YAMI is released under GNU GPLv3 license.\n"
				+ "See LICENSE.txt for more information.",
			"About YAMI",
			JOptionPane.INFORMATION_MESSAGE);
    }
}
